<?php 
namespace Admin\Controller;

class UserController extends AdminController{
	//数据查询
	public function index(){
		/*$data = M('user')->select();*/
		$data = new \Think\Model();
		$result=$data ->query('select u.id nid,u.name uname,u.display,u.utime,i.id iid,i.uid,i.email,i.phone,i.sex,i.birthday_year 
			from love_user u,love_userinfo i
			where u.id = i.uid');
	/*	$data = M('userinfo')->join('right join love_user on love_userinfo.uid = love_user.id')->field('love_user.name uname,love_user.display,love_user.utime,uid,email,phone,sex,birthday_year')->select();*/
/*		echo $data->getLastSql();*/
		/*var_dump($result);*/
		$this->assign('list',$result);
		$this->display();
	}
	//数据添加
	public function add(){
		$this->display();
	}
	//处理json数据
	public function deal(){
		$id = I('get.id/d',0);
	/*	var_dump($id);*/
		$district = M('district')->where("upid=$id")->select();
		
		// $dthis->assign('dis',$district);
		$this->ajaxReturn($district);
	}

	



	//数据添加操作
	public function insert(){
	/*	echo __ROOT__.'/Public/Images/touxiang.jpg';
		exit;*/
		define(TOUXIANG,__ROOT__.'/Public/Images/touxiang.jpg');
		$_POST['utime'] =time();
		$_POST['password'] = md5('123456');
		M('user')->create();
		$a=M('user')->add();
		if($a>0){
			$_POST['uid'] = $a;
			/*var_dump($_POST);*/
			M('userinfo')->create();
			if(M('userinfo')->add() > 0){
				/*echo M('userinfo')->getLastSql();
				exit;*/
				$data['uid'] = $_POST['uid'];
				$uid = $_POST['uid'];
				$data['name'] = __ROOT__.'/Public/Images/touxiang.jpg';
				$data['is_face'] = 1;
				M('userinfo')->integral = $_SESSION['rule'];
				$_POST['value'] = $_SESSION['rule'];
				$_POST['type'] = 1;
				$_POST['reason'] = '注册赠送积分';
				$_POST['utime'] = time();

				if(M('userphoto')->add($data) >0){
					if(M('userinfo')->where("uid=$uid")->save()>0){
							M('integral')->create();
							if(M('integral')->add()>0){
								$this->success('恭喜您，添加成功',U('index'));
							}else{
								$this->error('添加失败！');
							}
						
					}else{
						$this->error('添加失败！');
					}
					
				}else{
					$this->error('添加失败！');
				}
		}

		
	}
}
	//执行删除操作
	public function delete(){
		$iid = I('get.iid/d',0);
		$nid = I('get.nid/d',0);
		/*var_dump($iid);
		var_dump($nid);*/
		/*exit;*/
		$a = M('integral')->where("uid=$nid")->delete();
		if((M('userinfo')->delete("$iid") >0) && (M('user')->delete("$nid")>0 && $a>0)){
			$this->success('删除成功', U('index'));
		}else{
			$this->error('删除失败');
		}
	}

	//加载修改页面
	public function edit(){
		$iid = I('get.iid/d',0);
		$nid = I('get.nid/d',0);
		$data1 = M('user')->find($nid);
		$data2 = M('userinfo')->find($iid);

		if($data1 ==null || $data2 == null){
			$this->redirect('index');
			return;
		}
		var_dump($data1);
		var_dump($data2);

		$this->assign('data1',$data1);
		$this->assign('data2',$data2);
		$this->display();
	}


	//执行修改
	public function update(){
		$uid = I('get.uid/d',0);
		$iid = I('get.iid/d',0);
		/*dump ($uid);
		dump ($iid);
		dump($_POST);*/
		M('user')->create() ;   //创建数据 获取post数据；
		M('userinfo')->create() ;   //创建数据 获取post数据；
		M('user')->where("id=$uid")->save();
			if(M('userinfo')->where("id=$iid")->save()>0){
				$this->success('恭喜您，修改成功', U('index'));
			}else{
				$this->error('修改失败！');

			}
		

	}
	/*
	
	//修行修改
	public function update(){
		M('Student')->create();		//创建数据 获取post数据
		if (M('Student')->save() > 0) {
			$this->success('恭喜您，修改成功', U('index'));
		} else {
			$this->error('修改失败！');
		}
	}*/



	//查询用户相册信息
	//
	//
	

}